package com.enzoism.springboot.dive.bis.datastruct;

/**
 * 模拟队列
 * 特点：先进先出
 * 有两个核心方法
 * 1）enQueue 放入队列
 * 2）deQueuq 取出元素
 */
public class MyQueue {

    private MyNode first;
    private MyNode last;

    public static void main(String[] args) {
        MyNode data1 = new MyNode("data1");
        MyQueue myQueue = new MyQueue();
        myQueue.enQueue(data1);
        System.out.println("------>myQueue：" + myQueue);

        MyNode data2 = new MyNode("data2");
        myQueue.enQueue(data2);
        System.out.println("------>myQueue：" + myQueue);

        MyNode myNode = myQueue.deQueue();
        System.out.println("------>first：" + myNode);
        System.out.println("------>myQueue：" + myQueue);
        System.out.println();

        MyNode myNode2 = myQueue.deQueue();
        System.out.println("------>second：" + myNode2);
        System.out.println("------>myQueue：" + myQueue);

    }

    @Override
    public String toString() {
        return "MyQueue{" +
                "first=" + first +
                ", last=" + last +
                '}';
    }

    // 放入元素
    public void enQueue(MyNode node) {
        if (null == first) {
            first = node;
            last = first;
        } else {
            last.next = node;
            last = node;
        }
    }

    // 取出元素
    public MyNode deQueue() {
        if (null == first) {
            return null;
        } else {
            MyNode tempLast = new MyNode(first.data);
            first = first.next;
            // 注意：要做判断，负责last总是指向最后一个元素
            if (null== first){
                last = null;
            }
            return tempLast;
        }
    }

}
